package com.bluefish.bltuan.action.admin;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.action.AbstractAction;
import com.bluefish.bltuan.module.user.Manager;
import com.bluefish.bltuan.service.ManagerService;

@Results({
	@Result(name = "success", type = "dispatcher", location = "/page/admin/success.jsp")
})
public class ManagerAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8209441240925021806L;

	private Manager manager;
	
	private List<Manager> managers;
	
	private ManagerService managerService;
	
	public void setManagerService(ManagerService managerService) {
		this.managerService = managerService;
	}

	public Manager getManager() {
		return manager;
	}

	public void setManager(Manager manager) {
		this.manager = manager;
	}

	public List<Manager> getManagers() {
		return managers;
	}

	public void setManagers(List<Manager> managers) {
		this.managers = managers;
	}

	@Override
	public HttpHeaders index() {
		this.managers = managerService.getAll(getPageIndex());
		return new DefaultHttpHeaders(INDEX).disableCaching();
	}

	@Override
	public HttpHeaders show() {
		this.manager = this.managerService.findById(getIntegerId());
		return new DefaultHttpHeaders(SHOW).disableCaching();
	}

	@Override
	public HttpHeaders create() {
		this.manager.setRight();
		this.managerService.save(this.manager);
		return new DefaultHttpHeaders(SUCCESS).disableCaching();
	}

	@Override
	public HttpHeaders editNew() {
		return new DefaultHttpHeaders(NEW).disableCaching();
	}

	@Override
	public String update() {
		Manager manager = this.managerService.findById(getIntegerId());
		//copyProperties(this.manager, manager);
		manager.setRealname(this.manager.getRealname());
		this.managerService.update(manager);
		return SUCCESS;
	}

	@Override
	public String edit() {
		this.manager = this.managerService.findById(getIntegerId());
		return EDIT;
	}

	public String delete(){
		Manager manager = this.managerService.findById(getIntegerId());
		this.managerService.delete(manager);
		return SUCCESS;
	}
	
	@Override
	public String destroy() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String activate(){
		Manager manager = this.managerService.findById(getIntegerId());
		manager.activate();
		this.managerService.update(manager);
		return SUCCESS;
	}
	
	public String freeze(){
		Manager manager = this.managerService.findById(getIntegerId());
		manager.freeze();
		this.managerService.update(manager);
		return SUCCESS;
	}
}
